package java_thinking.unit_15._15_3;

import java.util.Iterator;

/**
 * @Description :适配器模式-对象适配器模式,在新类里面直接使用原始类进行组合返回所需功能
 *              练习7：使用组合代替继承
 * @author: cww
 * @DateTime: 2019/4/2 19:44
 */

public class IterableFibonacci2 implements Iterable<Integer>{

    private Fibonacci fibonacci = new Fibonacci();
    private int n;
    public IterableFibonacci2(int num) {
        n = num;
    }

    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return n >0;
            }

            @Override
            public Integer next() {
                --n;
                return fibonacci.next();
            }
        };
    }

    public static void main(String[] args) {
        IterableFibonacci2 it = new IterableFibonacci2(10);
        for (int i :it){
            System.out.print(i+" ");
        }
    }
}
